﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.IO;
using AutoUpdate.Common; 

namespace AutoUpdate
{
    public partial class frmKhoiDong : Telerik.WinControls.UI.RadForm
    {
    
        public frmKhoiDong()
        {
            InitializeComponent();
            updatetientrinh();
        }
      
        public void updatetientrinh()
        {
            string DuongDanIniFileClient = "", DuongDanUpdateDLLClident = "", DuongDanUpdateEXEClident = "", DuongDanUpdateREPORTClident = "", VersionUpdateDLLClident = "",
                VersionUpdateEXEClident = "", VersionUpdateREPORTClident = "", DuongDanUpdateDLLServer = "", DuongDanUpdateEXEServer = "", DuongDanUpdateREPORTServer = "",
                DuongDanIniFileServer = "", VersionUpdateDLLServer = "", VersionUpdateEXEServer = "", VersionUpdateREPORTServer = "";

            WriteReadIniFile iniclient = new WriteReadIniFile(Application.StartupPath+@"\update.ini");
            try
            {
                DuongDanIniFileClient = iniclient.IniReadValue("DuongDan", "DuongDanIniFileServer");
                DuongDanUpdateDLLClident = iniclient.IniReadValue("DuongDan", "DuongDanUpdateDLL");
                DuongDanUpdateEXEClident = iniclient.IniReadValue("DuongDan", "DuongDanUpdateEXE");
                DuongDanUpdateREPORTClident = iniclient.IniReadValue("DuongDan", "DuongDanUpdateREPORT");
                VersionUpdateDLLClident = iniclient.IniReadValue("Version", "VersionUpdateDLL");
                VersionUpdateEXEClident = iniclient.IniReadValue("Version", "VersionUpdateEXE");
                VersionUpdateREPORTClident = iniclient.IniReadValue("Version", "VersionUpdateREPORT");
            }
            catch
            {
                DuongDanIniFileClient = string.Empty;
                DuongDanUpdateDLLClident = string.Empty;
                DuongDanUpdateEXEClident = string.Empty;
                DuongDanUpdateREPORTClident = string.Empty;
                VersionUpdateDLLClident = string.Empty;
                VersionUpdateEXEClident = string.Empty;
                VersionUpdateREPORTClident = string.Empty;
            }
            if (File.Exists(String.Format(DuongDanIniFileClient)))
            {
                WriteReadIniFile iniServer = new WriteReadIniFile(DuongDanIniFileClient);
                try
                {

                    DuongDanIniFileServer = iniServer.IniReadValue("DuongDan", "DuongDanIniFileServer");
                    DuongDanUpdateDLLServer = iniServer.IniReadValue("DuongDan", "DuongDanUpdateDLL");
                    DuongDanUpdateEXEServer = iniServer.IniReadValue("DuongDan", "DuongDanUpdateEXE");
                    DuongDanUpdateREPORTServer = iniServer.IniReadValue("DuongDan", "DuongDanUpdateREPORT");
                    VersionUpdateDLLServer = iniServer.IniReadValue("Version", "VersionUpdateDLL");
                    VersionUpdateEXEServer = iniServer.IniReadValue("Version", "VersionUpdateEXE");
                    VersionUpdateREPORTServer = iniServer.IniReadValue("Version", "VersionUpdateREPORT");
                }
                catch
                {
                    DuongDanIniFileServer = string.Empty;
                    DuongDanUpdateDLLServer = string.Empty;
                    DuongDanUpdateEXEServer = string.Empty;
                    DuongDanUpdateREPORTServer = string.Empty;
                    VersionUpdateDLLServer = string.Empty;
                    VersionUpdateEXEServer = string.Empty;
                    VersionUpdateREPORTServer = string.Empty;
                }
            }
            else
            {
                DuongDanIniFileServer = string.Empty;
                DuongDanUpdateDLLServer = string.Empty;
                DuongDanUpdateEXEServer = string.Empty;
                DuongDanUpdateREPORTServer = string.Empty;
                VersionUpdateDLLServer = string.Empty;
                VersionUpdateEXEServer = string.Empty;
                VersionUpdateREPORTServer = string.Empty;
            }
            int trangthaicopyexe = 0;
            int trangthaicopydll = 0;
            int trangthaicopyreport = 0;
            if ((VersionUpdateDLLClident != VersionUpdateDLLServer) && !string.IsNullOrEmpty(VersionUpdateDLLServer))
            {
                trangthaicopydll = 1;
            }
            else
            {
                trangthaicopydll = 0;
            }
            if ((VersionUpdateEXEClident != VersionUpdateEXEServer) && !string.IsNullOrEmpty(VersionUpdateEXEServer))
            {
                trangthaicopyexe= 1;
            }
            else
            {
                trangthaicopyexe = 0;
            }
            if ((VersionUpdateREPORTClident != VersionUpdateREPORTServer) && !string.IsNullOrEmpty(VersionUpdateREPORTServer))
            {
                trangthaicopyreport = 1;
            }
            else
            {
                trangthaicopyreport = 0;
            }


       
            if (trangthaicopydll==1 || trangthaicopyexe==1 || trangthaicopyreport==1)
            {
                DialogResult kq = RadMessageBox.Show("Đã Có Phiên Bản Mới Của Chương Trình \n Bạn Có Muốn Cập Nhật Chương Trình Lên \n Version mới nhất không ?", "Thông Báo", MessageBoxButtons.YesNo, RadMessageIcon.Question);
                if (kq == DialogResult.Yes)
                {

                    try
                    {
                        TienTrinh tr = new TienTrinh(trangthaicopyexe,trangthaicopydll,trangthaicopyreport);
                        tr.ShowDialog();
                        this.Close();
                        //ProcessStartInfo startInfo = new ProcessStartInfo();
                        //startInfo.FileName = Application.StartupPath + @"\AutoUpdate.EXE";

                        // Process.Start(startInfo);

                    }
                    catch (Exception ex)
                    {
                        RadMessageBox.Show("Lỗi " + ex.ToString(), "Cảnh Báo", MessageBoxButtons.OK, RadMessageIcon.Error);
                        FormAo frAo = new FormAo();
                        frAo.ShowDialog();
                        this.Close();
                    }



                }
                else
                {
                    RadMessageBox.Show("Bạn đã hủy tiến trình cập nhật của chương trình !!! ", "Cảnh Báo", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                    FormAo frAo = new FormAo();
                    frAo.ShowDialog();
                    this.Close();
                }
            }
            else
            {

                FormAo frAo = new FormAo();
                frAo.ShowDialog();
                this.Close();

            }
        }

    }
}
